<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        class="filter-item"
        clearable
        @keyup.enter.native="handleFind"
        v-model="keyword"
        placeholder="请输入标签名"
      ></el-input>
      <el-button type="primary" @click="handleFind" icon="el-icon-search">查找</el-button>
      <el-button type="primary" @click="handleAdd" icon="el-icon-edit">添加</el-button>
      <el-button type="danger" @click="handleDeleteBatch" icon="el-icon-delete">删除</el-button>
    </div>

    <el-table :data="tableData" v-loading="Loading" style="width: 100%" @selection-change="handleSelectionChange" stripe>
      <el-table-column type="selection"></el-table-column>

      <el-table-column label="序号" width="60" align="center">
        <template slot-scope="scope">
          <span>{{scope.$index + 1}}</span>
        </template>
      </el-table-column>

      <el-table-column label="标签名" min-width="160" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>


      <el-table-column label="操作" fixed="right" min-width="230" align="center">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row)" type="primary" size="small">编辑</el-button>
          <el-button @click="handleDelete(scope.row)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <!-- 添加或修改对话框 -->
    <el-dialog
      :title="title"
      width="500px"
      :visible.sync="dialogFormVisible"
      :before-close="handleDialogClose"
      :close-on-click-modal='false'
    >
      <el-form label-position="left" label-width="70px" :model="form" :rules="rules" ref="form">
        <el-form-item label="标签名" prop="content">
          <el-input v-model="form.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleDialogClose">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  getTagList,
  stickTag,
  deleteBatchTag,
  tagSortByClickCount,
  tagSortByCite,
  addTag,
  editTag,
} from "@/api/tag";
export default {
  data() {
    return {
      Loading: true,
      multipleSelection: [], //多选，用于批量删除
      keyword: "",
      tableData: [],
      currentPage: 1,
      pageSize: 15,
      total: 0, //总数量
      title: "增加标签",
      dialogFormVisible: false,
      operationStatus: 0, //操作状态，1：新增，2：修改
      form: {
        name: ""
      },
      rules: {
        name: [
          { required: true, message: "分类名称不能为空", trigger: "blur" },
          { min: 1, max: 10, message: "长度在1到10个字符" },
        ]
      },
    };
  },
  created() {
    this.tagList();
  },
  methods: {
    tagList() {
      this.Loading = true;
      getTagList().then((res) => {
        let result = res.data
        if(result.code === 200){
          this.tableData = result.data.result || []
          this.total = 1
        }
        else
          alert(result.msg)
        this.Loading = false;
      });
    },
    handleFind() {
      this.tagList();
    },
    handleAdd() {
      this.title = "添加标签";
      this.dialogFormVisible = true;
      this.operationStatus = 1; //新增
    },
    handleDeleteBatch() {
      if (this.multipleSelection.length <= 0) {
        this.$message({
          type: "error",
          message: "请先选中需要删除的内容！",
        });
        return;
      }
      this.$confirm("此操作将把选中的标签删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$store.dispatch('app/setLoading', true);
          let params = []
          for(var tag of this.multipleSelection)
            params.push(tag.id)
          deleteBatchTag(params).then((response) => {
              if(response.data.code === 200)
              this.$message({
                type: "success",
                message: "删除成功",
              });
              else
                this.$message({
                  type: "error",
                  message: response.data.msg,
                });
            this.tagList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleEdit(row) {
      this.title = "编辑标签";
      this.dialogFormVisible = true;
      this.operationStatus = 2; //修改
      this.form = Object.assign(this.form, row);
    },
    handleDelete(row) {
      this.$confirm("此操作将把标签删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var params = [];
          params.push(row.id);
          this.$store.dispatch('app/setLoading', true);
          deleteBatchTag(params).then((response) => {
            this.tagList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleCurrentChange: function (val) {
      this.currentPage = val;
      this.tagList();
    },
    // 改变多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.operationStatus == 1) {
            //新增
            this.$store.dispatch('app/setLoading', true);
            addTag(this.form).then((response) => {
                this.$message({
                  type: "success",
                  message: response.data,
                });
                this.handleDialogClose();
                this.tagList();
            });
          } else if (this.operationStatus == 2) {
            //修改
            this.$store.dispatch('app/setLoading', true);
            editTag(this.form).then((response) => {
                this.$message({
                  type: "success",
                  message: response.data,
                });
                this.handleDialogClose();
                this.tagList();
            });
          }
        }
      });
    },
    //dialog关闭操作
    handleDialogClose() {
      this.form = {
        content: "",
        sort: 0,
      };
      this.dialogFormVisible = false;
    },
  },
};
</script>
